package com.mi.rof.engineer.service.impl;


import com.mi.rof.engineer.entity.RepairOrderEvents;
import com.mi.rof.engineer.entity.RepairOrderStates;
import com.mi.rof.engineer.entity.UploadInfoDTO;
import com.mi.rof.engineer.service.RepairOrderStateService;
import com.mi.rof.engineer.service.SmsService;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = Exception.class)
public class RepairOrderStateServiceImpl implements RepairOrderStateService {


    public static final String ORDER_ID_HEADER = "orderId";


    @Resource
    private RepairStateMachineBuild repairStateMachineBuild;


    @Resource
    private SmsService smsService;

    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> toPendingConfirm(String orderId) {
        return buildService1H(orderId, RepairOrderEvents.AFTER_SUBMIT);
    }

    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> uploadFaultInfo(UploadInfoDTO uploadInfoDTO) {
        String orderId = String.valueOf(uploadInfoDTO.getRepairOrderId());
        return buildService2H(orderId, RepairOrderEvents.UPLOAD_FAULT_INFO,"isOrderConfirmed",uploadInfoDTO.isOrderConfirmed());
    }


    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> preview(boolean isSkipApplyMaterials, String orderId) {
        return buildService2H(orderId, RepairOrderEvents.PRE_INSPECT,"isSkipApplyMaterials",isSkipApplyMaterials);
    }

    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> materialDistribution(boolean applyStatus, String orderId) {
        return buildService1H(orderId, RepairOrderEvents.MATERIAL_DISTRIBUTION);
    }

    /**
     * 工程师现场维修
     */
    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> repair(String orderId) {
        return buildService1H(orderId, RepairOrderEvents.REPAIR);
    }

    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> review(String orderId, boolean reviewStatus) {
        return buildService2H(orderId, RepairOrderEvents.REVIEW,"reviewStatus",reviewStatus);
    }

    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> pay(String orderId) {
        return buildService1H(orderId, RepairOrderEvents.PAY);
    }

    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> returnDevice(String orderId) {
        return buildService1H(orderId, RepairOrderEvents.RETURN);
    }
    @Override
    public StateMachine<RepairOrderStates, RepairOrderEvents> orderUpdate(String orderId) {
        return buildService1H(orderId, RepairOrderEvents.ORDER_UPDATE);
    }

    public StateMachine<RepairOrderStates, RepairOrderEvents> buildService1H(String orderId, RepairOrderEvents events){
        StateMachine<RepairOrderStates, RepairOrderEvents> stateMachine = repairStateMachineBuild.build(orderId);
        Message<RepairOrderEvents> uploadMessage = MessageBuilder.withPayload(events)
                .setHeader(ORDER_ID_HEADER, orderId)
                .build();
        stateMachine.sendEvent(uploadMessage);
        return stateMachine;
    }

    /**
     * 拥有两个header
     */
    public  StateMachine<RepairOrderStates, RepairOrderEvents> buildService2H(String orderId, RepairOrderEvents events,String headerName, Object headerValue){
        StateMachine<RepairOrderStates, RepairOrderEvents> stateMachine = repairStateMachineBuild.build(orderId);
        Message<RepairOrderEvents> uploadMessage = MessageBuilder.withPayload(events)
                .setHeader(ORDER_ID_HEADER, orderId)
                .setHeader(headerName, headerValue)
                .build();
        stateMachine.sendEvent(uploadMessage);
        return stateMachine;
    }


}
